<?php
// Kit Applicatif Php :: pile des evenements
/*

file version 2005-07-01 11:49:29

2005-07-01 10:36:51
	added arg resultTo to queue()
*/
class wakeventPile extends wakCoreComponent {
/*----------------------------------------------------------------------------------------------------
																														proprietes
----------------------------------------------------------------------------------------------------*/
	var $default_parameters=array();
	var $future=array(); // pile des evenements non encore declenches ('priority|evenement',...)
	var $past=array(); // pile des evenements declenches ('priority|evenement',...)
/*----------------------------------------------------------------------------------------------------
																												methodes publiques
----------------------------------------------------------------------------------------------------*/
	// charge la pile a partir d'un tableau (n => ('event' => event, 'priority' => priority, [other fields]))
	function loadPile($list) {
		$this->future=array();
		foreach ($list as $item) {
			//$this->future[]=$this->_makeName($item['event'],$item['priority']);
			$key=$this->_makeName($item['event'],$item['priority']);
			$this->future[$key]=$item;
		}
		// tri de la pile en ordre inverse
		krsort($this->future);
		//echo affvar($this->future);
	}
	// renvoie le prochain evenement, et le deplace de la pile future a past
	function getNext() {
		//echo affvar($this->future);
		if (count($this->future)==0) return false;
		$key=key($this->future);
		$next=array_shift($this->future);
		$this->past[$key]=$next;
		//$nextName=substr($next,4);
		//$nextName=$next['event'];
		//return $nextName;
		return $next;
	}
	function getNextEvent() {
		return $this->getNext();
	}

	function queue($event,$priority=1,$params=NULL,$resultTo=NULL) {
		// ajout
		$toQueue=$this->_makeName($event,$priority);
		if (!isset($this->future[$toQueue])) 
			$this->future[$toQueue]=array(
				'event'=>$event
				,'priority'=>$priority
				,'params'=>$params
				, 'result_to'=>$resultTo);
		// tri de la pile en ordre inverse
		krsort($this->future);
		//echo affvar($this->future);
	}
	function queueEvent($event,$priority=1,$params=NULL,$resultTo=NULL) {
		return $this->queue($event,$priority,$params,$resultTo);
	}
	
	function cancelEvent($event) {
		$this->C->log('vidage de la pile');
		return $this->cancel($event);
	}
	function cancel($event) {
		// on parcourt la pile des evts futurs en ne conservant que ceux qui ne sont pas annul øs
		$newfuture=array();
		foreach ($this->future as $step => $info) {
			if (substr($step,4)!=$event) $newfuture[$step]=$info;
			// on ne remet pas l'event dans la liste
		}
		$this->future=$newfuture;
	}
	// vide la pile next
	function cancelAll() {
		$this->future=array();
		return true;
	}
	
	function isEmpty() {
		if (count($this->future)==0) return true;
		else return false;
	}
	
	/*function replaceEvent($old,$event,$priority=) {
		$newfuture=array();
		foreach ($this->future as $step => $info) {
			if (substr($step,4)!=$old) $newfuture[]=$step;
			// on remplace old par new en conservant la priorite
			$newfuture[]=substr($step,0,4).$new;
		}
		$this->future=$newfuture;
	}*/
	
	function getFuturePile() {
		return $this->future;
	}
	
	function getPastPile() {
		$r=array();
		foreach ($this->past as $p)
			$r[]=$p['priority'].'|'.$p['event'].'|'.$p['params'];
		return $r;
		//return array_keys($this->past);
	}
	
	function isInFuture($event) {
		foreach ($this->future as $item => $info)
			if (substr($item,4)==$event) return true;
		return false;
	}
/*----------------------------------------------------------------------------------------------------
																												methodes privees
----------------------------------------------------------------------------------------------------*/
	function _makeName($event,$priority) {
		// formatage priorite 1 => 001
		$priority=sprintf("%03d",$priority);
		return $priority."|".$event;
	}
}
?>
